package com.xianbeiying.app.ckgl.widght;

import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import java.util.ArrayList;
import java.util.List;

//自定义折线   broken line
public class BrokenLineView{
    //	public static int[] COLORS = new int[] {R.color.pie_analysis_color1, R.color.pie_analysis_color2, R.color.pie_analysis_color3,
//			R.color.pie_analysis_color4, R.color.pie_analysis_color5 };
//    private LinearLayout view;
//
//    public BrokenLineView(Context context) {
//        super(context);
//        init(context);
//
//    }
//
//    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
//    public BrokenLineView(Context context, AttributeSet attrs, int defStyle) {
//        super(context, attrs, defStyle);
//        init(context);
//    }
//
//    public BrokenLineView(Context context, AttributeSet attrs) {
//        super(context, attrs);
//        init(context);
//    }
//
//    private void init(Context mContext) {
//    }
//
//    public void drawChartView(double[] value, String[] descrept, int[] COLORS) {
//        DrawPie(this, value, descrept, COLORS);
//    }

//    public void DrawPie(LinearLayout layout, double[] value, String[] descrept, int[] COLORS){
//        GraphicalView mChartView;
//        mChartView = ChartFactory.getPieChartView(layout.getContext(), mSeries, mRenderer);
//        layout.addView(mChartView, new LayoutParams(LayoutParams.WRAP_CONTENT,
//                LayoutParams.WRAP_CONTENT));
//    }
    /**
     * 获取坐标序列
     * @param size 序列点数
     * @param values  y轴值
     * @return  坐标x轴序列   或   坐标y轴序列
     */
    public static List<double[]> getlist(int size, List<String> values)
    {
        List<double[]> xy = new ArrayList<double[]>();

        double[] list =  new double[size];
        for(int i = 0 ; i < size ;i++)
        {
            list[i] = (values.isEmpty())? i : Double.valueOf(values.get(i));
        }

        xy.add(list);

        return xy;
    }


    /**
     * 构建XYMultipleSeriesRenderer.
     *
     * @param colors 每个序列的颜色
     * @param styles 每个序列点的类型(可设置三角,圆点,菱形,方块等多种)
     *                            ( PointStyle.CIRCLE, PointStyle.DIAMOND,PointStyle.TRIANGLE, PointStyle.SQUARE )
     * @return XYMultipleSeriesRenderer
     */
    public static XYMultipleSeriesRenderer buildRenderer(int[] colors, PointStyle[] styles)
    {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        // 控制横纵轴的属性字大小
        renderer.setAxisTitleTextSize(15);
        // 控制横纵轴的值大小
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setPointSize(5f);
        renderer.setMargins(new int[]
                { 20, 30, 15, 0 });
        int length = colors.length;
        for (int i = 0; i < length; i++)
        {
            XYSeriesRenderer r = new XYSeriesRenderer();
            r.setColor(colors[i]);
            r.setPointStyle(styles[i]);
            renderer.addSeriesRenderer(r);
        }
        return renderer;
    }

    /**
     * 设置renderer的一些坐标轴属性.
     *
     * @param renderer 要设置的renderer
     * @param title 图表标题
     * @param xTitle X轴标题
     * @param yTitle Y轴标题
     * @param xMin X轴最小值
     * @param xMax X轴最大值
     * @param yMin Y轴最小值
     * @param yMax Y轴最大值
     * @param axesColor X轴颜色
     * @param labelsColor Y轴颜色
     */
    public static void setChartSettings(XYMultipleSeriesRenderer renderer,
                                        String title, String xTitle, String yTitle, double xMin,
                                        double xMax, double yMin, double yMax, int axesColor,
                                        int labelsColor)
    {
        renderer.setChartTitle(title);
        renderer.setXTitle(xTitle);
        renderer.setYTitle(yTitle);
        renderer.setXAxisMin(xMin);
        renderer.setXAxisMax(xMax);
        renderer.setYAxisMin(yMin);
        renderer.setYAxisMax(yMax);
        renderer.setAxesColor(axesColor);
        renderer.setLabelsColor(labelsColor);
    }

    /**
     * 构建和时间有关的XYMultipleSeriesDataset,这个方法与buildDataset在参数上区别是需要List<Date[]>作参数.
     *
     * @param titles 序列图例
     * @param xValues X轴值
     * @param yValues Y轴值
     * @return XYMultipleSeriesDataset
     */
    public static XYMultipleSeriesDataset buildDataset(String[] titles,
                                                       List<double[]> xValues, List<double[]> yValues)
    {
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        int length = titles.length;
        for (int i = 0; i < length; i++)
        {
            XYSeries series = new XYSeries(titles[i]);
            double[] xV = xValues.get(i);
            double[] yV = yValues.get(i);
            int seriesLength = xV.length;
            for (int k = 0; k < seriesLength; k++)
            {
                series.add(xV[k], yV[k]);
            }
            dataset.addSeries(series);
        }
        return dataset;
    }
}